#include "StdAfx.h"
#include "EnumRegedit.h"
#include <Shlwapi.h>

#pragma comment(lib,"shlwapi.lib")

CEnumRegedit::CEnumRegedit(void)
{
}

CEnumRegedit::~CEnumRegedit(void)
{
}

//BOOL CEnumRegedit::FindVersionByKey(LPCTSTR strKey,LPCTSTR strPath,LPCTSTR strValue,UINT& nVersion)
BOOL CEnumRegedit::FindVersionByKey(LPCTSTR strKey,LPCTSTR strPath,LPCTSTR strValue,CStdString& strVersion)
{
	DWORD   dwType = REG_SZ;  
	DWORD   Len= MAX_PATH*2; 
	TCHAR   szVersion[MAX_PATH*2];
	memset(szVersion,0x0,MAX_PATH*2);
	HKEY    hKeyHandle;
	HKEY    hk; 
	if(StrCmpI(L"HKEY_CLASSES_ROOT",strKey) == 0)
	{
		hKeyHandle = HKEY_CLASSES_ROOT;
	}
	else if(StrCmpI(L"HKEY_CURRENT_CONFIG",strKey) == 0)
	{
		hKeyHandle = HKEY_CURRENT_CONFIG;
	}
	else if(StrCmpI(L"HKEY_CURRENT_USER",strKey) == 0)
	{
		hKeyHandle = HKEY_CURRENT_USER;
	}
	else if(StrCmpI(L"HKEY_USERS",strKey) == 0)
	{
		hKeyHandle = HKEY_USERS;
	}
	else if(StrCmpI(L"HKEY_LOCAL_MACHINE",strKey) == 0)
	{
		hKeyHandle = HKEY_LOCAL_MACHINE;
	}
	else
		return FALSE;
	RegOpenKey(hKeyHandle,strPath,&hk);  
	RegQueryValueEx(hk,strValue, 0,&dwType,(LPBYTE)szVersion,&Len);  
	RegCloseKey(hk); 
	//nVersion = _wtoi(szVersion);
	strVersion = szVersion;
	if(strVersion.GetLength() <= 0)
		return FALSE;

	return TRUE;
}

BOOL CEnumRegedit::FindVersion(LPCTSTR szProcess,CStdString& strVersionReturn)
{
	_ASSERT(szProcess);
	TCHAR szPath[MAX_PATH];
	memset(szPath,0x0,MAX_PATH);
	if(szProcess == NULL)
		return FALSE;
	_tcscpy(szPath,L"SoftWare\\Microsoft\\Windows\\CurrentVersion\\App Paths\\");
	_tcscat(szPath,szProcess);
	TCHAR szExePath[MAX_PATH*2];
	memset(szExePath,0x0,MAX_PATH*2);
	if(TRUE == FindExePath(szPath,szExePath))
	{
		return GetExeVersion(szExePath,strVersionReturn);
	}
	return FALSE;
}

BOOL CEnumRegedit::FindExePath(LPCTSTR szRegedit,TCHAR* szExePath)
{
	DWORD   dwType = REG_SZ;  
	DWORD   Len= MAX_PATH*2; 
	HKEY    hk; 
	RegOpenKey(HKEY_LOCAL_MACHINE,szRegedit,&hk);  
	RegQueryValueEx(hk,L"", 0,&dwType,(LPBYTE)szExePath,&Len);  
	RegCloseKey(hk); 
	if(_tcslen(szExePath) == 0)
		return FALSE;
	return TRUE;
}

//BOOL CEnumRegedit::GetExeVersion(LPCTSTR szFileName,UINT& nVersion)
BOOL CEnumRegedit::GetExeVersion(LPCTSTR szFileName,CStdString& strVersionReturn)
{
	_ASSERT(szFileName);
	TCHAR strVersion[MAX_PATH*2];
	memset(strVersion,0x0,MAX_PATH*2);
	DWORD dwVerSize; 
	DWORD dwHandle; 
	//TCHAR szFileName[MAX_PATH] = L"D:\\Program Files\\360\\360sd\\360sd.exe";
	TCHAR szVersion[MAX_PATH*2];
	memset(szVersion,0x0,MAX_PATH*2);
	dwVerSize = GetFileVersionInfoSize(szFileName, &dwHandle);    
	if(dwVerSize == 0)    
		return FALSE;    
	if(TRUE == GetFileVersionInfo(szFileName,NULL,MAX_PATH*2,szVersion))
	{
		VS_FIXEDFILEINFO * pInfo;    
		unsigned int nInfoLen;    

		if (VerQueryValue(szVersion, _T("\\"), (void**)&pInfo, &nInfoLen)) 
		{ 
			_sntprintf(strVersion,MAX_PATH,_T("%d.%d.%d.%d"),HIWORD(pInfo->dwFileVersionMS), LOWORD(pInfo->dwFileVersionMS),    
				HIWORD(pInfo->dwFileVersionLS), LOWORD(pInfo->dwFileVersionLS));
			///nVersion = _wtoi(strVersion);
			strVersionReturn = strVersion;
			return TRUE;    
		}
	}
	return FALSE;
}
